package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.admin.util.wxSendMessage;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallFormPurchase;
import org.linlinjava.litemall.db.service.LitemallPurchaseAuditService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/purchaseAudit")
@Validated
public class AdminPurchaseAuditController {

    @Autowired
    private wxSendMessage wxSendMessage;

    @Autowired
    private LitemallUserService litemallUserService;

    @Autowired
    private LitemallPurchaseAuditService purchaseAuditService;



    /**
     * 查询用户信息审批
     *
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:purchaseAudit:list")
    @RequiresPermissionsDesc(menu = {"信息审批", "采购信息审批"}, button = "查询")
    @GetMapping("/list")
    public Object list(String approveStatus, String sort,
                       String order, boolean selectAll,
                       String userType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit
    ) {

        return ResponseUtil.okList(purchaseAuditService.list(approveStatus,userType,sort,order,page,limit,selectAll));
    }

    /**
     * 通过审批
     *
     * @param purchase
     * @return
     */
    @RequiresPermissions("admin:purchaseAudit:approvalPurchaseAudit")
    @RequiresPermissionsDesc(menu = {"信息审批", "采购信息审批"}, button = "批准审批")
    @PostMapping("/approvalPurchaseAudit")
    @Transactional
    public Object approvalUserAudit(@RequestBody LitemallFormPurchase purchase) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        purchase.setStatus(Byte.valueOf("1"));
        purchase.setApproveUserId(loginAdmin.getId());
        purchase.setApproveTime(LocalDateTime.now());
        purchaseAuditService.updateById(purchase);
        //LitemallUser user = litemallUserService.queryMainByShopId(purchase.getShopId());

        return ResponseUtil.ok(purchase);
    }

    /**
     * 驳回审批
     *
     * @param purchase
     * @return
     */
    @RequiresPermissions("admin:purchaseAudit:rejectedPurchaseAudit")
    @RequiresPermissionsDesc(menu = {"信息审批", "采购信息审批"}, button = "驳回审批")
    @PostMapping("/rejectedPurchaseAudit")
    @Transactional(rollbackFor=Exception.class)
    public Object rejectedUserAudit(@RequestBody LitemallFormPurchase purchase) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        purchase.setStatus(Byte.valueOf("2"));
        purchase.setApproveUserId(loginAdmin.getId());
        purchase.setApproveTime(LocalDateTime.now());
        purchaseAuditService.updateById(purchase);
        return ResponseUtil.ok(purchase);
    }



}
